package com.rainng.coursesystem.manager.admin;

import com.rainng.coursesystem.dao.TeamDAO;
import com.rainng.coursesystem.dao.MemberEventDAO;
import com.rainng.coursesystem.dao.MemberDAO;
import com.rainng.coursesystem.manager.BaseManager;
import com.rainng.coursesystem.model.entity.TeamEntity;
import com.rainng.coursesystem.model.entity.MemberEntity;
import com.rainng.coursesystem.model.vo.response.IdNameVO;
import com.rainng.coursesystem.model.vo.response.table.MemberItemVO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MemberManager extends BaseManager {
    private final TeamDAO teamDAO;
    private final MemberEventDAO memberEventDAO;
    private final MemberDAO memberDAO;

    public MemberManager(TeamDAO teamDAO, MemberEventDAO memberEventDAO, MemberDAO memberDAO) {
        this.teamDAO = teamDAO;
        this.memberEventDAO = memberEventDAO;
        this.memberDAO = memberDAO;
    }

    public Integer getPageCount(String activityName, String teamName, String name) {
        int count = memberDAO.count(activityName, teamName, name);
        return calcPageCount(count, MemberDAO.PAGE_SIZE);
    }

    public List<MemberItemVO> getPage(Integer index, String activityName, String teamName, String name) {
        return memberDAO.getPage(index, activityName, teamName, name);
    }

    public MemberEntity get(Integer id) {
        return memberDAO.get(id);
    }

    public int create(MemberEntity entity) {
        return memberDAO.insert(entity);
    }

    public int update(MemberEntity entity) {
        return memberDAO.update(entity);
    }

    public int delete(Integer id) {
        return memberDAO.delete(id);
    }

    public TeamEntity getTeamById(Integer teamId) {
        return teamDAO.get(teamId);
    }

    public boolean hasMemberEvent(Integer memberId) {
        return memberEventDAO.countByMemberId(memberId) > 0;
    }

    public List<IdNameVO> listName() {
        List<IdNameVO> voList = new ArrayList<>();
        List<MemberEntity> entityList = memberDAO.listName();
        for (MemberEntity entity : entityList) {
            voList.add(new IdNameVO(entity.getId(), entity.getName()));
        }

        return voList;
    }
}
